import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;
import {Utils} from "./utils";

@ccclass
export default class GameSurpass extends cc.Component {
  @property(cc.Label)
  labelName: cc.Label = null
  @property(cc.Label)
  labelScore: cc.Label = null
  @property(cc.Sprite)
  spriteHead: cc.Sprite = null
  @property(cc.Node)
  wxDataDispatcher: cc.Node = null

  private lastOpenId: string = null

  onLoad() {
    this.wxDataDispatcher.on('get-score', (e) => {
      const data: any = e.detail
      Utils.findNext(parseInt(data.score)).then(res => {
        if (res.openid === this.lastOpenId) {
          this.node.active = true
          return
        }
        this.lastOpenId = res.openid
        if (res) {
          this.node.active = true
          this.labelName.string = res.nickname
          this.labelScore.string = res.score.toString()
          this.spriteHead.node.active = false
          Utils.loadRes(res.avatarUrl).then(res => {
            this.spriteHead.node.active = true
            this.spriteHead.spriteFrame = res
          })
        } else {
          this.node.active = false
        }
      })

    })
    // Utils.findNext()
  }
}